package org.example.leetcode;

/**
 * @author: lynn
 * @Descript:
 * @date: 2021/8/3 18:05
 * @version: 1.0
 */
public class LC38 {

    public static void main(String[] args) {
        LC38 lc=new LC38();
        System.out.println(lc.countAndSay(5));
    }
    //fixme https://www.bilibili.com/video/BV1ak4y1B7Ky?from=search&seid=8188089927938115068
    public String countAndSay(int n) {
        if (n==1) return "1";
        if (n==2) return "11";
        String[] dp=new String[n];
        dp[0]="1";
        dp[1]="11";
        for (int i = 2; i <n ; i++) {
            StringBuilder sb=new StringBuilder();
            int count=1;
            String pre=dp[i-1];
            char cur=pre.charAt(0);
            for (int j = 1; j <pre.length() ; j++) {
                if (cur!=pre.charAt(j)){
                    sb.append(count);
                    sb.append(cur);
                    cur=pre.charAt(j);
                    count=1;
                }else {
                    count++;
                }
            }
            sb.append(count);
            sb.append(cur);
            dp[i]=sb.toString();
        }
        return dp[n-1];
    }
}
